package com.mindata.blockmanager.controller.admin;

import com.mindata.blockmanager.bean.BaseData;
import com.mindata.blockmanager.bean.ResultGenerator;
import com.mindata.blockmanager.model.UserInfo;
import com.mindata.blockmanager.repository.UserInfoRepository;
import com.mindata.blockmanager.utils.ShiroMd5Util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * User: jasperxgwang
 * Date: 2018-8-8 14:21
 */
@Controller
@RequestMapping("/admin/user")
@Slf4j
public class UserAdminController extends BaseController {
    @Resource
    private UserInfoRepository userInfoRepository;

    /**
     * @param password
     * @param newPassword
     * @param newPassword2
     * @return
     */
    @RequestMapping("/updatePwd")
    @ResponseBody
    public BaseData updatePwd(String password, String newPassword, String newPassword2) {
        UserInfo userInfo = super.loginUserInfo();
        userInfo = userInfoRepository.findOne(userInfo.getUid());
        if (userInfo == null) {
            return ResultGenerator.genFailResult("用户不存在");
        }
        String pwdMd5 = ShiroMd5Util.getMd5Pwd(password, userInfo.getCredentialsSalt());
        if (!StringUtils.equals(pwdMd5, userInfo.getPassword())) {
            return ResultGenerator.genFailResult("原密码错误");
        }
        if (!StringUtils.equals(newPassword, newPassword2)) {
            return ResultGenerator.genFailResult("两次输入密码不一致");
        }
        userInfo.setPassword(ShiroMd5Util.getMd5Pwd(newPassword, userInfo.getCredentialsSalt()));
        userInfoRepository.save(userInfo);
        return ResultGenerator.genSuccessResult("修改成功");
    }
}
